package com.zjdiepu.www.manage.api.dh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.dh.DhInsuranceRenewalVoMapper;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceRenewalService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dh.dto.DhInsuranceRenewal;
import com.zjdiepu.www.model.dh.ext.DhInsuranceRenewalExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;

/**
 * 待续保列表Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月17日 10时36分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DhInsuranceRenewalController", produces="application/json;charset=UTF-8")
@Api(tags = {"待续保列表"}, produces="application/json;charset=UTF-8")
public class DhInsuranceRenewalController extends BaseController {
    @Resource
    private IDhInsuranceRenewalService dhInsuranceRenewalService;
    @Resource
    private DhInsuranceRenewalVoMapper dhInsuranceRenewalVoMapper;
    
    /**
     *   续保失败处理：将 续保状态更改为 90（不保）；
     * @param dhInsuranceRenewalExt
     * @return
     */
    @ApiOperation(value = "续保失败处理：将 续保状态更改为 90（不保）；",response = YwBusiness.class, httpMethod="POST", tags={"续保失败处理"})
    @RequestMapping(value = "nosaveInsurance", method = RequestMethod.POST)
    public ApiResponse nosaveInsurance(@ModelAttribute DhInsuranceRenewalExt dhInsuranceRenewalExt){
    	DhInsuranceRenewal dhInsuranceRenewal=new DhInsuranceRenewal();
    	dhInsuranceRenewal.setBusiId(dhInsuranceRenewalExt.getBusiId());
    	dhInsuranceRenewal.setDpStatus(DpStatus.NORMAL.getCode());
    	dhInsuranceRenewal=dhInsuranceRenewalService.selectRefined(dhInsuranceRenewal);
    	if(dhInsuranceRenewal!=null){
    		dhInsuranceRenewal.setStatusRenewal(90);
    		dhInsuranceRenewalService.saveUpdateAndDelete(dhInsuranceRenewal, OperateType.edit);
    	}
    	
    	return ApiResponse.success(true);
    }
    /**
     * @author xiongxiaotun <br/>
     * @Title: notAllocationList  <br/>
     * @Description: 查询未派单的续保 <br/>
     * @param dhInsuranceRenewalExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "查询未派单的续保",response = YwBusiness.class, httpMethod="POST", tags={"续保派单"})
    @RequestMapping(value = "notAllocationList", method = RequestMethod.POST)
    public ApiResponse notAllocationList(@ModelAttribute DhInsuranceRenewalExt dhInsuranceRenewalExt){
    	getCurrentUser();
        PageVo page = getPage(dhInsuranceRenewalExt);
        dhInsuranceRenewalExt.setBaseWhere();
        dhInsuranceRenewalExt.notAllocation();
        dhInsuranceRenewalExt.setDateDueCount(30);
        int records = dhInsuranceRenewalService.countByExt(dhInsuranceRenewalExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhInsuranceRenewal> dhInsuranceRenewals = dhInsuranceRenewalService.selectByExt(dhInsuranceRenewalExt);
            page.setRows(dhInsuranceRenewals);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: allocation <br/>
     * @Description: 派单<br/>
     * @param id
     * @param operatorId
     * @param operatorName
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "续保派单", response = Boolean.class, httpMethod="POST", tags={"续保派单"})
    @RequestMapping(value = "allocation", method = RequestMethod.POST)
    public ApiResponse allocation(String id, String operatorId, String operatorName) {
    	if(!org.apache.commons.lang3.StringUtils.isNoneBlank(id, operatorId, operatorName)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
        return dhInsuranceRenewalService.saveAllocation(id, operatorId, operatorName, currentUser);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: randomAllocation <br/>
     * @Description: 随机续保派单<br/>
     * @param allocationNum
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "随机续保派单", response = Boolean.class, httpMethod="POST", tags={"续保派单"})
    @RequestMapping(value = "randomAllocation", method = RequestMethod.POST)
    public ApiResponse randomAllocation(int allocationNum) {
    	SysUser currentUser =(SysUser)getCurrentUser();
    	dhInsuranceRenewalService.updateForRandomAllocation(allocationNum, currentUser);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: applayAllocation <br/>
     * @Description: 申请派单<br/>
     * @param allocationNum
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "申请续保派单", response = Boolean.class, httpMethod="POST", tags={"续保派单"})
    @RequestMapping(value = "applayAllocation", method = RequestMethod.POST)
    public ApiResponse applayAllocation(int allocationNum) {
    	SysUser currentUser =(SysUser)getCurrentUser();
    	dhInsuranceRenewalService.updateForApplyAllocation(allocationNum, currentUser);
        return ApiResponse.success(true);
    }

    @ApiOperation(value = "到期预警申请续保派单", response = Boolean.class, httpMethod="POST", tags={"续保派单"})
    @RequestMapping(value = "applyToAllCation", method = RequestMethod.POST)
    public ApiResponse applyllCation(int allocationNum) {
    	SysUser currentUser =(SysUser)getCurrentUser();
    	dhInsuranceRenewalService.updateApplyToAllCation(allocationNum, currentUser);
        return ApiResponse.success(true);
    }
    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增待续保列表 <br/>
     * @param dhInsuranceRenewal
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增待续保列表", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DhInsuranceRenewal dhInsuranceRenewal) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsuranceRenewal.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsuranceRenewal.setModifierName(currentUser.getName());
        dhInsuranceRenewalService.saveUpdateAndDelete(dhInsuranceRenewal, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除待续保列表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除待续保列表", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsuranceRenewalService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用待续保列表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用待续保列表", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsuranceRenewalService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用待续保列表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用待续保列表", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsuranceRenewalService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改待续保列表 <br/>
     * @param dhInsuranceRenewal
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改待续保列表", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhInsuranceRenewal dhInsuranceRenewal) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsuranceRenewal.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsuranceRenewal.setModifierName(currentUser.getName());
    	dhInsuranceRenewalService.saveUpdateAndDelete(dhInsuranceRenewal, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询待续保列表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询待续保列表", response = DhInsuranceRenewal.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DhInsuranceRenewal dhInsuranceRenewal = dhInsuranceRenewalService.selectById(id);
       if(null == dhInsuranceRenewal) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dhInsuranceRenewal);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询待续保列表 <br/>
     * @param dhInsuranceRenewalExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询待续保列表",response = DhInsuranceRenewal.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DhInsuranceRenewalExt dhInsuranceRenewalExt){
    	SysUser currentUser =(SysUser)getCurrentUser();
    	getCurrentUser();
        dhInsuranceRenewalExt.setBaseWhere();
        //只能查看被分配的任务
      //update 2017-7-13 查看所有，不需要派单
        dhInsuranceRenewalExt.setOperatorId(currentUser.getId());
        dhInsuranceRenewalExt.setOrderSql("`DATE_DUE` DESC");
        dhInsuranceRenewalExt.setDateDueCount(30);
        PageVo page = getPage(dhInsuranceRenewalExt);
        int records = dhInsuranceRenewalService.countByExt(dhInsuranceRenewalExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhInsuranceRenewal> dhInsuranceRenewals = dhInsuranceRenewalService.selectByExt(dhInsuranceRenewalExt);
            dhInsuranceRenewalVoMapper.mapper(dhInsuranceRenewals);
            page.setRows(dhInsuranceRenewals);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
